package com.ruoyi.web.controller.system;

import java.text.SimpleDateFormat;
import java.util.*;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.service.IDeptService;
import com.ruoyi.system.service.IDeptZhuanjiaService;
import com.ruoyi.web.controller.tool.JsonResult;
import com.ruoyi.web.controller.tool.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ZhuanjiaBiandongDetail;
import com.ruoyi.system.service.IZhuanjiaBiandongDetailService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import springfox.documentation.spring.web.json.Json;

/**
 * 专家变动详情Controller
 * 
 * @author ruoyi
 * @date 2022-12-08
 */
@Controller
@RequestMapping("/system/zhuanjiabiandongdetail")
public class ZhuanjiaBiandongDetailController extends BaseController
{
    private String ZJZXDeptId = RuoYiConfig.getZjzxdeptid();

    private String prefix = "system/zhuanjiabiandongdetail";

    @Autowired
    private IZhuanjiaBiandongDetailService zhuanjiaBiandongDetailService;

    @Autowired
    private IDeptZhuanjiaService deptZhuanjiaService;

    @Autowired
    private IDeptService deptService;

    @RequiresPermissions("system:zhuanjiabiandongdetail:view")
    @GetMapping()
    public String zhuanjiabiandongdetail(/*@RequestParam("piciid") String ppPiciId, */ModelMap ppMap)
    {
        /*ppMap.put("piciid",ppPiciId);*/
        return prefix + "/zhuanjiabiandongdetail";
    }

    //@RequiresPermissions("system:zhuanjiabiandongdetail:customview")
    @GetMapping("/customzhuanjiabiandongdetail")
    public String customzhuanjiabiandongdetail(/*@RequestParam("piciid") String ppPiciId, ModelMap ppMap*/)
    {
        /*ppMap.put("piciid",ppPiciId);*/
        return prefix + "/customzhuanjiabiandongdetail";
    }

    @RequiresPermissions("system:lishizhuanjiabiandongchaxun:view")
    @GetMapping("/lishizhuanjiabiandongchaxun")
    public String lishizhuanjiabiandongchaxun()
    {

        return "system/zhuanjiaguanli/lishizhuanjiabiandongchaxun/index";
    }

    @GetMapping("/customzhuanli")
    public String customzhuanli()
    {
        return "system/zhuanli/customzhuanli";
    }

    @GetMapping("/customzhuanjiabiandongdetail_shenhe")
    public String customzhuanjiabiandongdetail_shenhe(@RequestParam("piciid") String ppPiciId,@RequestParam("renwuid")String ppRenwuId, ModelMap ppMap)
    {
        ppMap.put("renwuid",ppRenwuId);
        ppMap.put("piciid",ppPiciId);
        return  "system/renwu/shenhedetail";
    }

    /**
     * 查询专家变动详情列表
     */
    @RequiresPermissions("system:zhuanjiabiandongdetail:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ZhuanjiaBiandongDetail zhuanjiaBiandongDetail)
    {
        startPage();
        List<ZhuanjiaBiandongDetail> list = zhuanjiaBiandongDetailService.selectZhuanjiaBiandongDetailList(zhuanjiaBiandongDetail);
        return getDataTable(list);
    }

    /**
     * 导出专家变动详情列表
     */
    @RequiresPermissions("system:zhuanjiabiandongdetail:export")
    @Log(title = "专家变动详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ZhuanjiaBiandongDetail zhuanjiaBiandongDetail)
    {
        List<ZhuanjiaBiandongDetail> list = zhuanjiaBiandongDetailService.selectZhuanjiaBiandongDetailList(zhuanjiaBiandongDetail);
        ExcelUtil<ZhuanjiaBiandongDetail> util = new ExcelUtil<ZhuanjiaBiandongDetail>(ZhuanjiaBiandongDetail.class);
        return util.exportExcel(list, "专家变动详情数据");
    }

    /**
     * 新增专家变动详情
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存专家变动详情
     */
    @RequiresPermissions("system:zhuanjiabiandongdetail:add")
    @Log(title = "专家变动详情", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ZhuanjiaBiandongDetail zhuanjiaBiandongDetail)
    {
        return toAjax(zhuanjiaBiandongDetailService.insertZhuanjiaBiandongDetail(zhuanjiaBiandongDetail));
    }

    /**
     * 修改专家变动详情
     */
    @RequiresPermissions("system:zhuanjiabiandongdetail:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        ZhuanjiaBiandongDetail zhuanjiaBiandongDetail = zhuanjiaBiandongDetailService.selectZhuanjiaBiandongDetailById(id);
        mmap.put("zhuanjiaBiandongDetail", zhuanjiaBiandongDetail);
        return prefix + "/edit";
    }

    /**
     * 修改保存专家变动详情
     */
    @RequiresPermissions("system:zhuanjiabiandongdetail:edit")
    @Log(title = "专家变动详情", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ZhuanjiaBiandongDetail zhuanjiaBiandongDetail)
    {
        return toAjax(zhuanjiaBiandongDetailService.updateZhuanjiaBiandongDetail(zhuanjiaBiandongDetail));
    }

    /**
     * 删除专家变动详情
     */
    //@RequiresPermissions("system:zhuanjiabiandongdetail:remove")
    @Log(title = "专家变动详情", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(zhuanjiaBiandongDetailService.deleteZhuanjiaBiandongDetailByIds(ids));
    }

    //删除专家变动详情
    @PostMapping("/deletebyid")
    @ResponseBody
    public JsonResult<?>deletebyid(@RequestParam("id")String ppId){
        try{
            int isDelete = zhuanjiaBiandongDetailService.deleteZhuanjiaBiandongDetailById(ppId);

            return isDelete==1?JsonResult.getSuccessResult("删除成功"):JsonResult.getErrorResult("删除失败");

        } catch (Exception e) {
            logger.error("zhuanjiabiandongdetail -> deletebyid: "+e.getMessage());
            return JsonResult.getErrorResult("删除变动失败");
        }
    }

    /**
     * 专家变动详情custom列表
     */
    @PostMapping("/custombiandonglist")
    @ResponseBody
    public JsonResult<?> custombiandonglist(@RequestParam("deptid")String ppDeptId,@RequestParam("zhuanjiamingcheng")String ppZhuanjiamingcheng,
                                                      @RequestParam("biandongleixing")String ppBiandongleixing,@RequestParam("biandongriqi")String ppBiandongriqi,
                                                       @RequestParam("pageindex") int ppPageIndex,
                                                       @RequestParam("pagesize") int ppPageSize)
    {
        try{
            List<Map<String,Object>> list = new ArrayList<>();
            int mmCount = 0;
            if (ppDeptId.equals(ZJZXDeptId)){
                list = zhuanjiaBiandongDetailService.selectZhuanjiaBiandongDetailListCustom("",ppZhuanjiamingcheng,ppBiandongleixing,ppBiandongriqi,ppPageIndex,ppPageSize);
                mmCount = zhuanjiaBiandongDetailService.countZhuanjiaBiandongDetailListCustom("",ppZhuanjiamingcheng,ppBiandongleixing,ppBiandongriqi);
            }else{
                list = zhuanjiaBiandongDetailService.selectZhuanjiaBiandongDetailListCustom(ppDeptId,ppZhuanjiamingcheng,ppBiandongleixing,ppBiandongriqi,ppPageIndex,ppPageSize);
                mmCount = zhuanjiaBiandongDetailService.countZhuanjiaBiandongDetailListCustom(ppDeptId,ppZhuanjiamingcheng,ppBiandongleixing,ppBiandongriqi);
            }

            PageInfo mmPageInfo = new PageInfo(ppPageIndex, ppPageSize, mmCount);
            Map<String, Object> mmMap = new HashMap<String, Object>();
            mmMap.put("biandonglist", list);
            mmMap.put("PageInfo", mmPageInfo);
            return JsonResult.getSuccessResult(mmMap);
        } catch (Exception e) {
            //log.error("工作站更名审核Controller -> findgengmingbytj: " + e.getMessage());
            return JsonResult.getErrorResult("查询失败");
        }
    }

    /**
     * 新增保存专家变动详情custom
     */
    //@RequiresPermissions("system:zhuanjiabiandongdetail:add")
    @Log(title = "专家变动详情", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    @ResponseBody
    public JsonResult<?> save(ZhuanjiaBiandongDetail zhuanjiaBiandongDetail)
    {
        try{
            String mmZhuanjiaId = zhuanjiaBiandongDetail.getZhuanjiaid();

            String mmBiandongId = zhuanjiaBiandongDetail.getId();

            String mmZhuanjiaxingming = deptZhuanjiaService.findByZhuanjiaId(mmZhuanjiaId).getXingming();

            //如果是专家转隶，取专家现单位ID
            String mmZhuanzidanweiId = "";
            if (zhuanjiaBiandongDetail.getBiandongleixing().equals("8")){
                mmZhuanzidanweiId = deptZhuanjiaService.findByZhuanjiaId(mmZhuanjiaId).getDeptid();
            }
            String mmZhuanzhidanweiId = zhuanjiaBiandongDetail.getZhuanzhidanwei()==null?"":zhuanjiaBiandongDetail.getZhuanzhidanwei();

            String mmZhuanzidanwei = "";
            String mmZhuanzhidanwei = "";

            if (!mmZhuanzidanweiId.equals("")){
                mmZhuanzidanwei = deptService.selectDeptById(mmZhuanzidanweiId).getJigoumingcheng();
            }

            if(!mmZhuanzhidanweiId.equals("")){
                mmZhuanzhidanwei = deptService.selectDeptById(mmZhuanzhidanweiId).getJigoumingcheng();
            }

            zhuanjiaBiandongDetail.setZhuanjiamingcheng(mmZhuanjiaxingming);
            zhuanjiaBiandongDetail.setZhuanzidanwei(mmZhuanzidanweiId);
            zhuanjiaBiandongDetail.setZhuanzidanweimingcheng(mmZhuanzidanwei);
            zhuanjiaBiandongDetail.setZhuanzhidanweimingcheng(mmZhuanzhidanwei);

            //添加时间
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            zhuanjiaBiandongDetail.setBeizhu2(sdf.format(new Date()));

            int isOk = 0;

            String mmZhuanjiabiandongDetailId = "";

            //新增
            if (mmBiandongId==null||mmBiandongId.equals("")){

                //查询是否已存在

                if (zhuanjiaBiandongDetailService.existBiandongByZhuanjiaIdAndBeizhu1(mmZhuanjiaId).size()!=0){
                    return JsonResult.getErrorResult("已存在该专家的变动，请勿重复提交。");
                }

                mmZhuanjiabiandongDetailId = UUID.randomUUID().toString();
                zhuanjiaBiandongDetail.setId(mmZhuanjiabiandongDetailId);
                zhuanjiaBiandongDetail.setIsused((long)0);
                zhuanjiaBiandongDetail.setIsvalid((long)1);

                isOk = zhuanjiaBiandongDetailService.insertZhuanjiaBiandongDetailCustom(zhuanjiaBiandongDetail)==true?1:0;
            }
            //修改
            else if(mmBiandongId!=null){
                mmZhuanjiabiandongDetailId = mmBiandongId;

                isOk = zhuanjiaBiandongDetailService.updateZhuanjiaBiandongDetail(zhuanjiaBiandongDetail);
            }

            return isOk==1?JsonResult.getSuccessResult(mmZhuanjiabiandongDetailId):JsonResult.getErrorResult("保存失败");

        } catch (Exception e) {
            logger.error("zhuanjiabiandongdetail -> save: "+e.getMessage());
            return JsonResult.getErrorResult("添加变动失败");
        }
    }

    @Log(title = "专家变动详情", businessType = BusinessType.INSERT)
    @PostMapping("/wubiandongqueren")
    @ResponseBody
    public JsonResult<?> wubiandongqueren(@RequestParam("deptid")String ppDeptId)
    {
        try{
            ZhuanjiaBiandongDetail zhuanjiaBiandongDetail = new ZhuanjiaBiandongDetail();

            zhuanjiaBiandongDetail.setId(UUID.randomUUID().toString());
            zhuanjiaBiandongDetail.setDeptid(ppDeptId);
            zhuanjiaBiandongDetail.setZhuanjiamingcheng("无变动确认");
            zhuanjiaBiandongDetail.setBiandongriqi(new Date());
            zhuanjiaBiandongDetail.setShenpishijian(new Date());
            zhuanjiaBiandongDetail.setBeizhu1("1");
            zhuanjiaBiandongDetail.setIsused((long)0);
            zhuanjiaBiandongDetail.setIsvalid((long)1);

            int isOk = zhuanjiaBiandongDetailService.insertZhuanjiaBiandongDetailCustom(zhuanjiaBiandongDetail)==true?1:0;

            return isOk==1?JsonResult.getSuccessResult("保存成功"):JsonResult.getErrorResult("保存失败");

        } catch (Exception e) {
            logger.error("zhuanjiabiandongdetail -> save: "+e.getMessage());
            return JsonResult.getErrorResult("添加变动失败");
        }
    }

    @PostMapping("/shenheone")
    @ResponseBody
    public JsonResult<?> shenheone(@RequestParam("id") String ppId,@RequestParam("shenhejieguo")String ppShenhejieguo,@RequestParam("shenheyijian")String ppShenheyijian)
    {
        try{
            ZhuanjiaBiandongDetail mmZhuanjiabiandongDetail = zhuanjiaBiandongDetailService.selectZhuanjiaBiandongDetailById(ppId);
            if (mmZhuanjiabiandongDetail==null){
                return JsonResult.getErrorResult("查询失败");
            }
            mmZhuanjiabiandongDetail.setBeizhu1(ppShenhejieguo);
            mmZhuanjiabiandongDetail.setBeizhu3(ppShenheyijian);

            return zhuanjiaBiandongDetailService.updateZhuanjiaBiandongDetail(mmZhuanjiabiandongDetail)==1?JsonResult.getSuccessResult("审核成功"):JsonResult.getErrorResult("审核失败");

        } catch (Exception e) {
            logger.error("zhuanjiabiandongdetail -> shenheone: "+e.getMessage());
            return JsonResult.getErrorResult("查询专家变动失败");
        }
    }

    @PostMapping("/findone")
    @ResponseBody
    public JsonResult<?> findone(@RequestParam("id") String ppId)
    {
        try{
            ZhuanjiaBiandongDetail mmZhuanjiabiandongDetail = zhuanjiaBiandongDetailService.selectZhuanjiaBiandongDetailById(ppId);

            return mmZhuanjiabiandongDetail==null?JsonResult.getErrorResult("查询失败"):JsonResult.getSuccessResult(mmZhuanjiabiandongDetail);

        } catch (Exception e) {
            logger.error("zhuanjiabiandongdetail -> findone: "+e.getMessage());
            return JsonResult.getErrorResult("查询专家变动失败");
        }
    }


    /**
     * 专家管理-当批次专家变动状态管理-历史专家变动查询-列表
     */
    @RequiresPermissions("system:lishizhuanjiabiandongchaxun:list")
    @PostMapping("/lishizhuanjiabiandongList")
    @ResponseBody
    public JsonResult<?> lishizhuanjiabiandongList(@RequestParam("deptid")String ppDeptId,@RequestParam("zhuanjiamingcheng")String ppZhuanjiamingcheng,
                                            @RequestParam("biandongleixing")String ppBiandongleixing,@RequestParam("biandongriqi")String ppBiandongriqi,
                                            @RequestParam("cengji")String cengji,@RequestParam("danweimingcheng")String danweimingcheng,
                                            @RequestParam("pageindex") int ppPageIndex,
                                            @RequestParam("pagesize") int ppPageSize)
    {
        try{

            List<Map<String,Object>> list = zhuanjiaBiandongDetailService.lishizhuanjiabiandongList(ppDeptId,ppZhuanjiamingcheng,ppBiandongleixing,ppBiandongriqi,danweimingcheng,cengji,ppPageIndex,ppPageSize);
            int mmCount = zhuanjiaBiandongDetailService.lishizhuanjiabiandongListCount(ppDeptId,ppZhuanjiamingcheng,ppBiandongleixing,ppBiandongriqi,danweimingcheng,cengji);


            PageInfo mmPageInfo = new PageInfo(ppPageIndex, ppPageSize, mmCount);
            Map<String, Object> mmMap = new HashMap<String, Object>();
            mmMap.put("biandonglist", list);
            mmMap.put("PageInfo", mmPageInfo);
            return JsonResult.getSuccessResult(mmMap);
        } catch (Exception e) {
            return JsonResult.getErrorResult("查询失败");
        }
    }
}
